/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package comunications;

import businessLogic.PlayerActions;
import crashManager.CrashManager;
import gameState.Player;
import gameState.Ring;
import gameState.Table;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DEX
 */

public class SendTable extends Thread{

        private Player player;
        private Table table;
        private Ring ring = Ring.getInstance();
        Logger logger = Logger.getLogger(this.getClass().getSimpleName());


        public SendTable(Table t, Player p) {
                this.player=p;
                this.table=t;
                logger.setLevel(Level.ALL);
        }


        public void run() {
            if(!ring.amIalone()){
                try{
                        logger.warning("[SendTable] ah che bello, mandiamo il tavolo a: "+player.getName());
                        Registry registry = LocateRegistry.getRegistry(player.getIp(), player.getPort());
                        ClientComunicateInterface cci = (ClientComunicateInterface)registry.lookup("ClientComunicateImpl");
                        logger.warning("[SendTable] di chi e' questa cci? "+cci.quiParloIo());
                        logger.warning("[SendTable] table.getCurrentPlayer().getName(): "+table.getCurrentPlayer().getName());
                        cci.bringTable(table);
                        logger.warning("[SendTable] tavolo inviato a "+player.getIp());
                }
                catch(Exception e){
                    logger.warning("chi ha fatto la cacca, l'ha fatta perche': "+e.getCause().toString());
                    e.printStackTrace();
                    CrashManager cm = new CrashManager();
                    cm.solveNext();
                    try{this.sleep(200);}catch(Exception e2){e2.printStackTrace();}

                    try{
                        Ring ring = Ring.getInstance();
                        Thread st = new SendTable(table, ring.getMyNextPlayer());
                        st.start();
                    }
                    catch(Exception qualcosa){
                        logger.warning("[SendTable] Exception qualcosa catturata!");
                    }
                }
            }
            else{
                logger.warning("[SendTable] secondo questo Ring.getInstance() sono rimasto da solo, per cui niente SendTable...");
                PlayerActions pa = new PlayerActions(player);
                pa.iAmLegend();
            }   
        }
}

